package com.ls.controller;

import com.alibaba.druid.support.spring.stat.annotation.Stat;
import com.google.code.kaptcha.Producer;
import com.ls.dto.UserDto;
import com.ls.service.UserService;
import com.ls.utils.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
@RequestMapping("user")
@Api(value = "UserController",tags = "用户管理")
public class UserController {

    @Autowired
    private Producer captchaProducer;

    @Autowired
    private UserService userService;

    /**
     * 跳转登录页面
     * @return
     */
    @ApiOperation(value = "跳转登录页面")
    @GetMapping("/toLogin")
    public String toLogin(){

        return "login";
    }

    @GetMapping("logout")
     public String logout(HttpSession session){
        session.removeAttribute("user");
        return "login";
    }

    /**
     * 登录接口
     * @param
     * @return
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    @ResponseBody
    public Status login(UserDto userDto,HttpSession session){

        //获取session中的验证码
        String captcha = (String) session.getAttribute("capCode");

        //判断是否含有验证码
        if (userDto.getHasCode()){
            //判断验证码错误
            System.out.println("==========验证码错误===========");
            if (!userService.codeIsTrue(userDto.getCode(),captcha)){
                return new Status(false,"验证码错误");
            }
        }

        return userService.login(userDto,session);
    }

    /**
     * 验证码
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取验证码")
    @GetMapping("/getCode")
    public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        // create the text for the image
        String capText = captchaProducer.createText();
        System.out.println("******************验证码是: " + capText + "******************");
        // store the text in the session
        request.getSession().setAttribute("capCode", capText);
        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        // write the data out
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }


    /**
     * 跳转登录页面
     * @return
     */
    @ApiOperation(value = "跳转注册页面")
    @GetMapping("/register")
    public String toRegister(){
        return "register";
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("/addUser")
    @ResponseBody
    public Status addUser(UserDto userDto){
        return  userService.insertResult(userDto);
    }


    /**
     * 修改头像
     * @param file
     * @param session
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "修改头像")
    @PostMapping("/updAvatar")
    public Status upload(@RequestParam("file") MultipartFile file, HttpSession session) throws IOException {
        return userService.uploadImage(file, session);
    }
}
